package com.ktgj.customer.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.CouponBO;
import com.ktgj.customer.domain.CouponDto;
import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.vo.CouponVo;
import com.ktgj.customer.domain.vo.HotelCouponVo;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.Coupon;
import com.ktgj.customer.service.ICouponService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 优惠券信息Controller
 * 
 * @author ruoyi
 * @date 2021-08-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/coupon")
public class CouponController extends BaseController
{
    @Autowired
    private ICouponService couponService;

    @Autowired
    private ILoginService loginService;

    /**
     * 查询优惠券信息列表
     */
    @PreAuthorize(hasPermi = "customer:coupon:list")
    @GetMapping("/list")
    public TableDataInfo list(Coupon coupon)
    {
        startPage();
        List<Coupon> list = couponService.selectCouponList(coupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券信息列表
     */
    @PreAuthorize(hasPermi = "customer:coupon:export")
    @Log(title = "优惠券信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupon coupon) throws IOException
    {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "优惠券信息数据");
    }

    /**
     * 获取优惠券信息详细信息
     */
    @PreAuthorize(hasPermi = "customer:coupon:query")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId)
    {
        return AjaxResult.success(couponService.selectCouponById(couponId));
    }

    /**
     * 新增优惠券信息
     */
    @PreAuthorize(hasPermi = "customer:coupon:add")
    @Log(title = "优惠券信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改优惠券信息
     */
    @PreAuthorize(hasPermi = "customer:coupon:edit")
    @Log(title = "优惠券信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠券信息
     */
    @PreAuthorize(hasPermi = "customer:coupon:remove")
    @Log(title = "优惠券信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(couponService.deleteCouponByIds(couponIds));
    }

    /**
     * 根据金额推荐最优优惠券
     * @param request
     * @return
     */
    //@SignatureValidation
    @PostMapping("/queyUserCoupon")
    public AjaxResult queyUserCoupon(HttpServletRequest request, @RequestBody Map<String, BigDecimal> map) {
        BigDecimal money = map.get("money");
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        CouponDto coupon = couponService.queyUserCoupon(customer.getCustomerId(), money);
        if (coupon != null)
            return AjaxResult.success(coupon);
        else
            return AjaxResult.error("该用户没有可用的优惠券");
    }

    /**
     * 根据金额推荐最优优惠券
     * @param token
     * @return
     */
    @PostMapping("/queyUserCouponByToken")
    public AjaxResult queyUserCouponByToken(@RequestParam("token") String token, @RequestParam("money") BigDecimal money) {
        Customer customer;
        try {
            customer = loginService.getLoginCustomerByToken(token);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        CouponDto coupon = couponService.queyUserCoupon(customer.getCustomerId(), money);
        if (coupon != null)
            return AjaxResult.success(coupon);
        else
            return AjaxResult.error("该用户没有可用的优惠券");
    }

    /**
     * 根据金额推荐最优优惠券(CRM)
     * @param customerId
     * @return
     */
    @PostMapping("/queyUserCouponById")
    public AjaxResult queyUserCouponById(@RequestParam("customerId") Long customerId, @RequestParam("money") BigDecimal money) {
        CouponDto coupon = couponService.queyUserCoupon(customerId, money);
        if (coupon != null)
            return AjaxResult.success(coupon);
        else
            return AjaxResult.error("该用户没有可用的优惠券");
    }

    /**
     * 优惠券使用记录列表
     * */
    //@SignatureValidation
    @PostMapping("/couponRecord")
    public AjaxResult couponRecord(HttpServletRequest request, @RequestBody Map<String, Object> map){
        String type = null;
        if (map.get("type") != null && !map.get("type").equals("")){
            type = map.get("type").toString();
        }

        Long customerId;
        if (map.get("id") != null && !map.get("id").equals("")){
            customerId = Long.valueOf(map.get("id").toString());
        }
        else {
            Customer customer;
            try {
                customer = loginService.getLoginCustomer(request);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            customerId = customer.getCustomerId();
        }

        return AjaxResult.success(couponService.couponRecord(customerId, type));
    }

    /**
     * 优惠券列表
     * */
    //@SignatureValidation
    @PostMapping("/CustomerRightdetailList")
    public AjaxResult CustomerRightdetailList(HttpServletRequest request, @RequestBody Map<String, Object> map){
        String type = map.get("type").toString();
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(couponService.CustomerRightdetailList(customer.getCustomerId(), type));
    }

    /**
     * 远程调用-优惠券金额
     * @param couponId
     * @return
     */
    @PostMapping("/couponById")
    public AjaxResult couponById(@RequestParam("couponId") Long couponId) {
        Coupon coupon = couponService.couponById(couponId);
        return AjaxResult.success(coupon);
    }

    /**
     * 根据会员权益id查询优惠卷信息
     */
    @PostMapping("/couponByCustomerRightDetailId")
    public AjaxResult couponByCustomerRightDetailId(@RequestParam("customerRightDetailId") Long customerRightDetailId) {
        CouponBO couponBO = couponService.couponByCustomerRightDetailId(customerRightDetailId);
        return AjaxResult.success(couponBO);
    }

    /**
     * 查询指定业务线的优惠券信息
     * */
    @PostMapping("/businessCoupon")
    public AjaxResult businessCoupon(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (map.get("businessType") != null && !map.get("businessType").equals("")){
            String businessType = map.get("businessType").toString();
            List<CouponVo> couponVos;
            try {
                couponVos = couponService.businessCoupon(businessType, customerId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (couponVos != null && couponVos.size() > 0){
                return AjaxResult.success(couponVos);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 酒店优惠券列表
     * */
    @PostMapping("/couponList")
    public List<HotelCouponVo> couponList(@RequestBody Map<String, Object> map){
        String type = map.get("type").toString();
        List<HotelCouponVo> hotelCouponVos = couponService.couponList(type);
        return hotelCouponVos;
    }
}
